package com.serwer.gui;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.SystemColor;
import java.awt.Color;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

public class NowyIndex extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable table;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					String admin = null;
					NowyIndex frame = new NowyIndex(admin);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public NowyIndex(String admin) {
		setTitle("mSerwer - nowy index");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 356, 275);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon("C:\\Users\\marmicc\\Desktop\\PRO\\Broker\\ico\\server.png"));
		label.setBounds(10, 189, 48, 48);
		contentPane.add(label);
		
		JLabel lblNewLabel = new JLabel("Zalogowany jako:");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel.setBounds(55, 223, 91, 14);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("New label");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel_1.setBounds(145, 223, 86, 14);
		lblNewLabel_1.setText(admin);
		contentPane.add(lblNewLabel_1);
		
		JButton button = new JButton("Zamknij okno");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Window frame = SwingUtilities.windowForComponent((Component) e
	                    .getSource());
	            frame.setVisible(false);
			}
		});
		button.setFont(new Font("Tahoma", Font.PLAIN, 11));
		button.setBounds(216, 155, 114, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("Zamknij program");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			System.exit(0);
			}
		});
		button_1.setFont(new Font("Tahoma", Font.PLAIN, 11));
		button_1.setBounds(216, 189, 116, 23);
		contentPane.add(button_1);
		
		JLabel lblNowyIndex = new JLabel("Nowy index");
		lblNowyIndex.setForeground(SystemColor.textHighlight);
		lblNowyIndex.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNowyIndex.setBounds(10, 11, 147, 19);
		contentPane.add(lblNowyIndex);
		
		JLabel label_1 = new JLabel("mSerwer");
		label_1.setForeground(new Color(0, 128, 128));
		label_1.setFont(new Font("Tahoma", Font.BOLD, 26));
		label_1.setBounds(181, 5, 116, 23);
		contentPane.add(label_1);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 41, 136, 135);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setShowVerticalLines(false);
		table.setShowHorizontalLines(false);
		table.setShowGrid(false);
		table.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent arg0) {
				DefaultTableModel model=(DefaultTableModel) table.getModel();
		        model.setRowCount(0);
		        String sql="SELECT index_name FROM Indexy";			                 
		        try {			            
		            Class.forName("com.mysql.jdbc.Driver");
		            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
		            Statement stmt=con.createStatement();
		            ResultSet rs = stmt.executeQuery(sql);

		            while (rs.next())
		            {
		                String nazwa= rs.getString("index_name");
		                model.addRow(new Object []{
		                   nazwa
		                });			    
		            }
		        }
		        catch(Exception ee) {
		         JOptionPane.showMessageDialog(null, ee.getMessage());
		        }
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Indexy w bazie"
			}
		) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			boolean[] columnEditables = new boolean[] {
				false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(0).setPreferredWidth(100);
		scrollPane.setViewportView(table);
		
		JLabel lblNewLabel_3 = new JLabel("Nowy index:");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel_3.setBounds(156, 63, 68, 14);
		contentPane.add(lblNewLabel_3);
		
		textField = new JTextField();
		textField.setBounds(216, 60, 114, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("Dodaj");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				  String nazwa_indexu = textField.getText();
				               try{
				            Class.forName("com.mysql.jdbc.Driver");
				             Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
				    Statement stmt=con.createStatement();
				           String insertStr="insert into indexy (index_name) values("
				                    +quotate(nazwa_indexu)+");";
				            
				            System.out.println(insertStr);
				            stmt.executeUpdate(insertStr);	
				            DefaultTableModel model=(DefaultTableModel) table.getModel();
			                model.addRow(new Object []{				
			                		nazwa_indexu
			    	         });  
				        }
				        catch(Exception e){
				            e.printStackTrace();
				        }
			}
		});
		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton.setBounds(216, 99, 114, 23);
		contentPane.add(btnNewButton);
	}
	public String quotate(String content){	    
        return "'"+content+"'";
    }
}
